package com.engine4cloud.tdf.supervision.controller.web;

import cn.hutool.core.collection.CollUtil;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.log.annotation.SysLog;
import com.engine4cloud.tdf.supervision.dto.DcpgEvaluateDTO;
import com.engine4cloud.tdf.supervision.dto.IdentityUniqueCheckDTO;
import com.engine4cloud.tdf.supervision.dto.create.DcpgInvestigationCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.DcpgInvestigationQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.DcpgInvestigationResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.DcpgInvestigationUpdateDTO;
import com.engine4cloud.tdf.supervision.service.DcpgInvestigationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 调查评估-委托-web
 *
 * @author wenjie
 * @date 2024-02-29 18:04:50
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/web/investigation")
@Tag(description = "InvestigationWeb", name = "调查评估-委托-web")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class DcpgInvestigationWebController {

    private final DcpgInvestigationService dcpgInvestigationService;

    /**
     * 分页查询
     *
     * @param dto 调查评估-委托-请求对象
     * @return R<PageInfo < DcpgInvestigationResultDTO>>
     */
    @Operation(summary = "分页查询", description = "分页查询：动作与状态对应关系：CREATE->县级待指派；COUNTRY_ASSIGN->所待接收；VILLAGE_ACCEPT->所待提交；VILLAGE_REFUSE->所拒绝;VILLAGE_SUBMIT->县待同意;COUNTRY_APPROVE->待交付;COUNTRY_REJECT->县拒绝；DELIVERY->已入矫；REFUSE_RECEIVE->入矫拒绝")
    @GetMapping("/page")
    public R<PageInfo<DcpgInvestigationResultDTO>> getInvestigationPage(@Valid @ParameterObject DcpgInvestigationQueryDTO dto) {
        return R.ok(dcpgInvestigationService.pageList(dto));
    }


    /**
     * 通过id查询调查评-委托信息
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R<DcpgInvestigationResultDTO> getById(@PathVariable("id") Long id) {
        return R.ok(dcpgInvestigationService.getById(id));
    }

    /**
     * 证件号是否唯一
     *
     * @param dto dto
     * @return 返回 true-唯一；false-不唯一
     */
    @Operation(summary = "证件号是否唯一", description = "证件号是否唯一")
    @PostMapping("/check")
    public R<Boolean> checkUnique(@Valid @RequestBody IdentityUniqueCheckDTO dto) {
        return R.ok(dcpgInvestigationService.checkUniqueByIdentity(dto));
    }

    /**
     * 新增调查评估-委托信息
     *
     * @param dto 调查评估信息表
     * @return R
     */
    @Operation(summary = "新增调查评估信息表", description = "新增调查评估-委托信息")
    @SysLog("新增调查评估-委托信息")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('investigation_add')")
    public R<Boolean> create(@Valid @RequestBody DcpgInvestigationCreateDTO dto) {
        return R.ok(dcpgInvestigationService.create(dto));
    }

    /**
     * 修改调查评估-委托信息
     *
     * @param dto 调查评估信息表
     * @return R
     */
    @Operation(summary = "修改调查评估-委托信息", description = "修改调查评估-委托信息")
    @SysLog("修改调查评估-委托信息")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('investigation_edit')")
    public R<Boolean> updateById(@Valid @RequestBody DcpgInvestigationUpdateDTO dto) {
        return R.ok(dcpgInvestigationService.update(dto));
    }

    /**
     * 调查评估-结果意见（所填写）
     *
     * @param dto 调查评估-结果意见（所填写）
     * @return R 权限：和编辑是一个权限
     */
    @Operation(summary = "调查评估-结果意见（所填写）", description = "调查评估-结果意见（所填写）")
    @SysLog("调查评估-结果意见")
    @PutMapping("/evaluate")
    @PreAuthorize("@pms.hasPermission('investigation_edit')")
    public R<Boolean> evaluate(@Valid @RequestBody DcpgEvaluateDTO dto) {
        return R.ok(dcpgInvestigationService.evaluate(dto));
    }

    /**
     * 通过id删除调查评估-委托信息
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除调查评估-委托信息", description = "通过id删除调查评估-委托信息")
    @SysLog("通过id删除调查评估-委托信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('investigation_del')")
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        return R.ok(dcpgInvestigationService.removeBatchByIds(CollUtil.toList(ids)));
    }
}